
FPrate_M <- function(org, res) {
    ret <- 0
    n <- length(unique(org))

    for (i in unique(org)) {
        temp = sum((org == i) - (res == i) < 0) / sum((org == i) == 0)

        if (is.na(temp) | temp == 0) {
            n = n-1
        }
        else {
            ret = ret + temp
        }
    }
    return(ret/n)
}

TPrate_M <- function(org, res) {
    ret <- 0
    n <- length(unique(org))

    for (i in unique(org)) {
        temp = sum((org == i) + (res == i) == 2) / sum((org == i) == 1)

        if (is.na(temp) | temp == 0) {
            n = n-1
        }
        else {
            ret = ret + temp
        }
    }
    return(ret/n)
}

precision_M <- function(org, res) {
    ret <- 0
    n <- length(unique(org))

    for (i in unique(org)) {
        temp = sum((org == i) + (res == i) == 2) / sum((res == i) == 1)

        if (is.na(temp) | temp == 0) {
            n = n-1
        }
        else {
            ret = ret + temp
        }
    }
    return(ret/n)
}

TA <- function(org, res) {
    ret <- 0
    n <- length(unique(org))

    for (i in unique(org)) {
        ret = ret + sum((org == i) == (res == i)) / length(org)
    }
    return(ret/n)
}

sensitivity_M <- function(org, res) {
    return(TPrate_M(org, res))
}

specificity_M <- function(org, res) {
    return(1 - FPrate_M(org, res))
}

Fmeasure <- function(org, res) {
    p <- precision_M(org, res)
    r <- TPrate_M(org, res)

    return((2*p*r)/(p+r))
}

